using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.DomainEntities.BuyQualification.DomainService;
using SOEI.Solution.DomainEntities.BuyQualification.Dtos;
using SOEI.Solution.DomainEntities.Parameters.DomainService;
using SOEI.Solution.DomainEntities.Parameters.Dtos;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.DomainEntities.BuyQualification
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：购资审核申请应用服务<br/>
    /// 【创建日期】  ：2020.08.11 <br/>
    /// 【开发人员】  ：static残影<br/>
    ///</summary>
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = false)]
    public class BuyQualificationApplyAppService : SolutionAppServiceBase, IBuyQualificationApplyAppService
    {
        /// <summary>
        ///【BuyQualificationApply】仓储层
        /// </summary>
        private readonly IRepository<BuyQualificationApply, long> _buyqualificationapplyRepository;

        /// <summary>
        ///【BuyQualificationApply】领域服务
        /// </summary>
        private readonly IBuyQualificationApplyManager _buyqualificationapplyManager;

        /// <summary>
        /// 【Parameter】参数的领域服务
        /// </summary>
        private readonly IParameterManager _parameterManager;

        public BuyQualificationApplyAppService(
            IRepository<BuyQualificationApply, long> buyqualificationapplyRepository,
            IBuyQualificationApplyManager buyqualificationapplyManager,
            IParameterManager parameterManager
        )
        {
            _buyqualificationapplyRepository = buyqualificationapplyRepository;
            _buyqualificationapplyManager = buyqualificationapplyManager;
            _parameterManager = parameterManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        ///【BuyQualificationApply】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<PagedResultDto<BuyQualificationApplyListDto>> GetPaged(GetBuyQualificationApplysInput input)
        {
            var query = _buyqualificationapplyRepository.GetAll()
                          //模糊搜索 字段Name
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Name.Contains(input.FilterText))
                          //模糊搜索 字段PhoneNumber
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.PhoneNumber.Contains(input.FilterText))
                          //模糊搜索 字段CertificateType
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CertificateType.Contains(input.FilterText))
                          //模糊搜索 字段IdNumber
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.IdNumber.Contains(input.FilterText))
                          //模糊搜索 字段MarriageCondition
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.MarriageCondition.Contains(input.FilterText))
                          //模糊搜索 字段IfCensusregister
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.IfCensusregister.Contains(input.FilterText))
                          //模糊搜索 字段BuyerCondition
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.BuyerCondition.Contains(input.FilterText))
                          //模糊搜索 字段OwnPropertyCount
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.OwnPropertyCount.Contains(input.FilterText))
                          //模糊搜索 字段EnterPeople
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.EnterPeople.Contains(input.FilterText))
                          //模糊搜索 字段ApplyStatus
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ApplyStatus.Contains(input.FilterText))
                          //模糊搜索 字段AuditResult
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.AuditResult.Contains(input.FilterText))
            ;
            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<BuyQualificationApplyListDto>>(entityList);

            return new PagedResultDto<BuyQualificationApplyListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【BuyQualificationApply】通过指定id获取MemberListDto信息
        /// </summary>
        [AbpAuthorize()]
        public async Task<BuyQualificationApplyListDto> GetById(EntityDto<long> input)
        {
            var entity = await _buyqualificationapplyRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<BuyQualificationApplyListDto>(entity);
            return dto;
        }

        /// <summary>
        ///【BuyQualificationApply】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetBuyQualificationApplyForEditOutput> GetForEdit(NullableIdDto<long> input)
        {
            //var userEntity = await this.GetPersonChecker();

            var output = new GetBuyQualificationApplyForEditOutput();

            //编辑时候选择下拉框-->证件类型
            var queryCertificateType = this._parameterManager.QueryCertificateType();
            var CertificateList = await queryCertificateType
                .AsNoTracking()
                .ToListAsync();
            output.CertificateTypes = ObjectMapper.Map<ICollection<ParameterListDto>>(CertificateList);


            //编辑时候选择下拉框-->买受人购房情况类型
            var queryBuyerConditionType = this._parameterManager.QueryBuyerConditionType();
            var BuyerConditionList = await queryBuyerConditionType.AsNoTracking().ToListAsync();
            output.BuyQualificationTypes = ObjectMapper.Map<ICollection<ParameterListDto>>(BuyerConditionList);

            //编辑时候选择下拉框-->户籍所在类型
            var queryCensusRegisterType = this._parameterManager.QueryCensusRegisterType();
            var CensusRegisterList = await queryCensusRegisterType.AsNoTracking().ToListAsync();
            output.CensusRegisterTypes = ObjectMapper.Map<ICollection<ParameterListDto>>(CensusRegisterList);


            BuyQualificationApplyEditDto editDto;

            if (input.Id.HasValue)
            {
                var entity = await _buyqualificationapplyRepository.GetAsync(input.Id.Value);
                editDto = ObjectMapper.Map<BuyQualificationApplyEditDto>(entity);
            }
            else
            {
                editDto = new BuyQualificationApplyEditDto();
            }
            output.BuyQualificationApply = editDto;
            return output;
        }
        /// <summary>
        ///【BuyQualificationApply】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<BuyQualificationApplyEditDto> CreateOrUpdate(CreateOrUpdateBuyQualificationApplyInput input)
        {

            if (input.BuyQualificationApply.Id.HasValue)
            {
                var dto = await Update(input.BuyQualificationApply);
                return dto;
            }
            else
            {
                var dto = await Create(input.BuyQualificationApply);
                return dto;
            }
        }
        /// <summary>
        ///【BuyQualificationApply】新增
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<BuyQualificationApplyEditDto> Create(BuyQualificationApplyEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增  

            var entity = ObjectMapper.Map<BuyQualificationApply>(input);
            //调用领域服务
            entity = await _buyqualificationapplyManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<BuyQualificationApplyEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【BuyQualificationApply】编辑
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<BuyQualificationApplyEditDto> Update(BuyQualificationApplyEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id.Value;
            var entity = await _buyqualificationapplyRepository.GetAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            entity = await _buyqualificationapplyManager.UpdateAsync(entity);

            var dto = ObjectMapper.Map<BuyQualificationApplyEditDto>(entity);

            return dto;
        }

        /// <summary>
        ///【BuyQualificationApply】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task Delete(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _buyqualificationapplyManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【BuyQualificationApply】 批量删除Member的方法
        /// </summary>
        [AbpAuthorize()]
        public async Task BatchDelete(List<long> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _buyqualificationapplyManager.BatchDelete(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/
        #endregion
    }
}
